<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use EasyWeChat\Foundation\Application;
use EasyWeChat\MiniProgram\Encryption\Encryptor;
use EasyWeChat\MiniProgram\Notice\Notice;
use EasyWeChat\MiniProgram\AccessToken;

class Index extends Api
{
    protected $app = '';

    protected $open_id = '';

    protected $options = [
            'debug'  => false,
            'app_id'  => 'wx5a1090e16a2f3e95',         // AppID
            'secret'  => '7d7f576e91c8e8dc1af4fbeb6938ac2a',     // AppSecret
            'token'   => 'i9ch3f2ca',          // Token
            'aes_key' => 'RBuTcKRnHXbrLr4mrjQJW16ezM0UlggeHJZfu09ecnf',                    // 
            'oauth' => [
                'scopes'   => ['snsapi_base'],
                'callback' => '/user/callback',
            ],
            /**
             * 微信支付
             */
            'payment' => [
                'merchant_id'        => '1419891902',
                'key'                => '845d5f1153c27beed29f479640445148',
                'cert_path'          => 'path/to/your/cert.pem', // XXX: 绝对路径！！！！
                'key_path'           => 'path/to/your/key',      // XXX: 绝对路径！！！！
            ],
            /**
             * Guzzle 全局设置
             *
             * 更多请参考： http://docs.guzzlephp.org/en/latest/request-options.html
             */
            'guzzle' => [
                'timeout' => 3.0, // 超时时间（秒）
                'verify' => false, // 关掉 SSL 认证（强烈不建议！！！）
            ],
            /**
             * 小程序
             */
            'mini_program' => [
                'token' => '',
                'aes_key' => '',
                'app_id'  => 'wx3bdfa8264cbf60e5',         // AppID
                'secret'  => 'e7e23067297b7ba54b5667001ab2a624',     // AppSecret
            ]
        ];

    protected $beforeActionList = [
        'checkToken' =>  ['except'=>'gettoken,getsess']
    ];
    
    /**
     * 获取token
     * @return [type] [description]
     */
    public function getToken()
    {
        $user_name = $this->request->header('mobile');
        $password  = $this->request->header('password');
        $sign      = $this->request->header('sign');

        if ($this->checkSign(['sign' => $sign, 'mobile' => $user_name, 'password' => $password])) {
            if ($user = model('Users')->login($user_name, $password)) {
                return ['code' => 200, 'data' => $this->createToken($user->visible(['id'])->toJson())];
            }
        }
        return ['code' => 201, 'data' => 'error'];
    }
    public function getLogin()
    {
        $this->app = new Application($this->options);
        $session = $this->app->mini_program->user->getSessionKey($this->request->get('code'));
        return (new Encryptor($session->session_key, $this->request->get('iv')))->decryptData($this->request->get('decryptData'));
    }
    public function getNotice()
    {
        $touser = $this->request->get('touser');
        $form_id = $this->request->get('formId');
        $data = [
            'touser' => $touser,
            'template_id' => 'p7FLbk3D4KdnLyGYzmF5VPKSLao5H74HyzJLqlPRRjs',
            'page' => '',
            'form_id' => $form_id,
            'data' => [
                "keyword1" => [
                      "value" => "100000元", 
                      "color" => "#173177"
                  ], 
                  "keyword2" => [
                      "value" => "高兴!!!", 
                      "color" => "#173177"
                  ]
            ],
            'emphasis_keyword' => 'keyword1.DATA',
        ];
        $this->app = new Application($this->options);
        return $this->app->mini_program->notice->send($data);
    }
    public function getTest()
    {
        return model('Orders')->getOrders();
        return $this->getLoginUserInfo();
    }
}
